package org.jash.shop2202.user

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import androidx.databinding.ObservableArrayMap
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import org.jash.shop2202.R
import org.jash.shop2202.databinding.ActivityLoginBinding
import org.jash.shop2202.mvvm.BaseActivity
import org.jash.shop2202.utils.StoreUtils
import org.jash.shop2202.utils.logging
@Route(path = "/shop/login")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    val checkPassword = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$".toRegex()
    val user by lazy { ObservableArrayMap<String, String>().apply {
        put("username", StoreUtils.getUtils().getString("username", ""))
        put("password", StoreUtils.getUtils().getString("password", ""))
    }
    }
    override fun initData() {

        viewModel.successLiveData.observe(this) {
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
            logging(it)
            StoreUtils.getUtils().put("token", it.token)
            StoreUtils.getUtils().put("username", user["username"] ?: "")
            StoreUtils.getUtils().put("password", user["password"] ?: "")
            ARouter.getInstance().build("/shop/main").navigation()
            finish()
        }
        ARouter.getInstance().inject(this)
    }

    override fun initView() {
        binding.user = user
        binding.checkPassword = checkPassword
        binding.login.setOnClickListener {
            viewModel.login(user)
        }
//        binding.password.editText?.addTextChangedListener {
//            binding.password.error = "密码必须大于8位大写字母"
//            binding.password.isErrorEnabled = !checkPassword.matches(it.toString())
//            binding.login.isEnabled = !binding.password.isErrorEnabled
//        }
    }
}